from sqlalchemy import delete, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_sale.entity.do.packaging_do import SalePackaging
from module_sale.entity.vo.packaging_vo import PackagingModel, PackagingPageQueryModel
from utils.page_util import PageUtil


class PackagingDao:
    """
    包装费用管理模块数据库操作层
    """

    @classmethod
    async def get_packaging_detail_by_id(cls, db: AsyncSession, packaging_id: int):
        """
        根据包装方式记录id获取包装费用管理详细信息

        :param db: orm对象
        :param packaging_id: 包装方式记录id
        :return: 包装费用管理信息对象
        """
        packaging_info = (
            (
                await db.execute(
                    select(SalePackaging)
                    .where(
                        SalePackaging.packaging_id == packaging_id
                    )
                )
            )
            .scalars()
            .first()
        )

        return packaging_info

    @classmethod
    async def get_packaging_detail_by_info(cls, db: AsyncSession, packaging: PackagingModel):
        """
        根据包装费用管理参数获取包装费用管理信息

        :param db: orm对象
        :param packaging: 包装费用管理参数对象
        :return: 包装费用管理信息对象
        """
        packaging_info = (
            (
                await db.execute(
                    select(SalePackaging).where(
                        SalePackaging.packaging_id == packaging.packaging_id if packaging.packaging_id else True,
                    )
                )
            )
            .scalars()
            .first()
        )

        return packaging_info

    @classmethod
    async def get_packaging_list_by_info(cls, db: AsyncSession, packaging: PackagingModel):
        """
           根据查询参数获取包装费用管理列表信息

           :param db: orm对象
           :param query_object: 查询参数对象
           :return: 包装费用管理列表信息对象
        """
        query = (
            select(SalePackaging)
            .where(
                SalePackaging.product_id == packaging.product_id if packaging.product_id else True,
                SalePackaging.year == packaging.year if packaging.year else True,
                SalePackaging.as_id == packaging.as_id if packaging.as_id else True,
            )
            .order_by(SalePackaging.packaging_id)
            .distinct()
        )
        packaging_list = await PageUtil.paginate(db, query, 1, 30, False)

        return packaging_list

    @classmethod
    async def get_packaging_list(cls, db: AsyncSession, query_object: PackagingPageQueryModel, is_page: bool = False):
        """
        根据查询参数获取包装费用管理列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 包装费用管理列表信息对象
        """
        query = (
            select(SalePackaging)
            .where(
                SalePackaging.product_id == query_object.product_id if query_object.product_id else True,
                SalePackaging.year == query_object.year if query_object.year else True,
            )
            .order_by(SalePackaging.packaging_id)
            .distinct()
        )
        packaging_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return packaging_list

    @classmethod
    async def add_packaging_dao(cls, db: AsyncSession, packaging: PackagingModel):
        """
        新增包装费用管理数据库操作

        :param db: orm对象
        :param packaging: 包装费用管理对象
        :return:
        """
        db_packaging = SalePackaging(**packaging.model_dump(exclude={'packaging_id', 'del_status'}))
        db.add(db_packaging)
        await db.flush()

        return db_packaging

    @classmethod
    async def edit_packaging_dao(cls, db: AsyncSession, packaging: dict):
        """
        编辑包装费用管理数据库操作

        :param db: orm对象
        :param packaging: 需要更新的包装费用管理字典
        :return:
        """
        await db.execute(update(SalePackaging), [packaging])

    @classmethod
    async def delete_packaging_dao(cls, db: AsyncSession, packaging: PackagingModel):
        """
        删除包装费用管理数据库操作

        :param db: orm对象
        :param packaging: 包装费用管理对象
        :return:
        """
        await db.execute(delete(SalePackaging).where(SalePackaging.packaging_id.in_([packaging.packaging_id])))

